package com.shishuo.cdn.filter;

import java.io.IOException;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.dotnet.common.utils.PropertiesUtil;
import com.shishuo.cdn.entity.User;
import com.shishuo.cdn.util.ClientIPUtils;

public class BusinessFilter implements Filter {

	private Log log = LogFactory.getLog(getClass());

	// 不需要过滤的地址,例:index.do
	// 在web.xml中配置，多个以逗号隔开
	private String nofilterStr;

	@Override
	public void destroy() {
		System.out.println("BussinessFilter--->销毁");
	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) resp;
		HttpSession session = request.getSession();

		// 访问路径
		String servletPath = request.getServletPath();
		String login = "/user/login.htm";

		//System.out.println("BussinessFilter--->过滤：" + servletPath);// +" - "+((HttpServletRequest)servletRequest).getLocalName()+
		// 访问路径
		// System.out.println("-00-0-");
		// " - "+((HttpServletRequest)servletRequest).getLocalPort()+
		// " - "+((HttpServletRequest)servletRequest).getLocalAddr());
		Map<String, String> properties;
		try {
			properties = PropertiesUtil.getInstance("/config.properties");
			String ip=properties.get("ip");
			request.getSession().setAttribute("ip", ip);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		String s[] = nofilterStr.split(",");

		
		System.out.println("用户IP：" + ClientIPUtils.getIpAddr(request));
		
		User user = (User) session.getAttribute("user");
		log.info("请求链接：" + servletPath);
		log.info("请求参数：" + request.getQueryString());
		
		boolean flag = false;
		for (int i = 0; i < s.length; i++) {
			if (servletPath.contains(s[i])) {
				flag = true;
			}
		}
		if (flag) {
			System.out.println("webXml里配置放行的url：-->"+servletPath);
			chain.doFilter(req, resp);
		} else {
			if (null != user) {
				System.out.println("已经登录，放行-->"+servletPath);
				chain.doFilter(req, resp);
			} else {
				System.out.println("未登录，被拦截-->"+servletPath);
				response.sendRedirect(request.getContextPath() + login);
			}
		}
	}

	@Override
	public void init(FilterConfig cfg) throws ServletException {
		System.out.println("BussinessFilter--->初始化");
		/** 获取web.xml配置的文件信息 */
		String str = cfg.getInitParameter("nofilterStr");
		if (!StringUtils.isBlank(str)) {
			nofilterStr = str;
		}
	}

}
